<?php
	require_once "../config/app.config.php";
    if(isset($_REQUEST["definition"]) && $_REQUEST["definition"] == "true"){
    	$entity = new EntityBLL();
		$EntityDefinition = array();
		//$entityFields = $entity->GetStoreAsJSON($_REQUEST["__id"]);
    	//vp($entityFields);
		$EntityDefinition['Store'] = $entity->GetStoreAsJSON($_REQUEST["__id"]);
		$EntityDefinition['ColumnModel'] = $entity->GetColumnModelAsJSON($_REQUEST["__id"]);
		$jsonResult = "(". json_encode($EntityDefinition) .")";
		print $jsonResult;
    }else{
    	$entity = new EntityBLL();
		$filterStr = "";
		if(isset($_REQUEST["query"]) && trim($_REQUEST["query"]) != ""){
			$filterStr = trim($_REQUEST["query"]);
		}
    	$result = $entity->GetRecordsAsJSON($_REQUEST["__id"], $_REQUEST["start"], $_REQUEST["limit"], $filterStr);
		//vp($arr);
		$jsonresult = json_encode($result["Records"]);
		print '({"total":"'.$result["TotalRecords"].'","results":'.$jsonresult.'})';
    	/*
		mysql_connect("localhost", "root", "") or
		   die("Could not connect: " . mysql_error());
		mysql_select_db("domainone_new");
		$query = "SELECT * FROM presidents pr, parties pa WHERE pr.IDparty = pa.IDparty";
		// Here we check if we have a query parameter :
		if (isset($_POST['query'])){
			$query .= " AND (pr.firstname LIKE '%".addslashes($_POST['query'])."%' OR pr.lastname LIKE '%".addslashes($_POST['query'])."%')";
		}
		$result = mysql_query($query);
		$nbrows = mysql_num_rows($result);
		$start = (integer) (isset($_POST['start']) ? $_POST['start'] : $_GET['start']);
		$end = (integer) (isset($_POST['limit']) ? $_POST['limit'] : $_GET['limit']);
		$limit = $query." ORDER BY IDpresident LIMIT ".$start.",".$end;	
		//vp($limit);	
		$result = mysql_query($limit); 	
		if($nbrows>0){
			while($rec = mysql_fetch_assoc($result)){
			    // render the right date format
				//$rec['tookoffice']=codeDate($rec['tookoffice']);
				//$rec['leftoffice']=codeDate($rec['leftoffice']);    
				$arr[] = $rec;
			}
			//vp($arr);
			$jsonresult = json_encode($arr);
			echo '({"total":"'.$nbrows.'","results":'.$jsonresult.'})';
		} else {
			echo '({"total":"0", "results":""})';
		}*/
    }
?>